//
//  THColorView.m
//  duolaCard
//
//  Created by 希达 on 2018/5/8.
//  Copyright © 2018年 zyy. All rights reserved.
//

#import "THColorView.h"
#import "THFilterModel.h"

@interface THColorCell : UICollectionViewCell
@property (nonatomic, weak) UIButton *colorBtn;
@property (nonatomic, strong) THColorModel *model;

@end

@implementation THColorCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *btn = [[UIButton alloc] initWithFrame:self.contentView.bounds];
        btn.userInteractionEnabled = NO;
        [self.contentView addSubview:btn];
        self.colorBtn = btn;
    }
    return self;
}

- (void)setModel:(THColorModel *)model {
    _model = model;
    [self.colorBtn setImage:[UIImage imageNamed:model.color_icon_def] forState:UIControlStateNormal];
    [self.colorBtn setImage:[UIImage imageNamed:model.color_icon_sel] forState:UIControlStateSelected];
    self.colorBtn.selected = model.isSelected;
}

@end


@interface THColorView() <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) UICollectionView *collectionView;
@property (weak, nonatomic) UIButton *selectBtn;
@end

@implementation THColorView

- (void)dealloc {
    NSLog(@"%@释放了", NSStringFromClass(self.class));
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(30, 30);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        collectionView.backgroundColor = [UIColor clearColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.showsVerticalScrollIndicator = YES;
        collectionView.showsHorizontalScrollIndicator = YES;
        [collectionView registerClass:[THColorCell class] forCellWithReuseIdentifier:@"THColorCell"];
        [self addSubview:collectionView];
        self.collectionView = collectionView;
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.colorArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    THColorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"THColorCell" forIndexPath:indexPath];
    cell.model = self.colorArray[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    THColorModel *model = self.colorArray[indexPath.item];

    if (indexPath.item == 0 && ![model.colorName containsString:@"#"]) {
        model.isSelected = !model.isSelected;
        if (model.isSelected) {
            if ([model.colorName containsString:@"Italic"]) {
                model.colorName = [model.colorName stringByReplacingOccurrencesOfString:@"-Italic" withString:@"-BoldItalic"];
            } else {
                model.colorName = [model.colorName stringByReplacingOccurrencesOfString:@"MT" withString:@"-BoldMT"];
            }
        } else {
            if ([model.colorName containsString:@"Italic"]) {
                model.colorName = [model.colorName stringByReplacingOccurrencesOfString:@"Bold" withString:@""];
            } else {
                model.colorName =[model.colorName stringByReplacingOccurrencesOfString:@"-Bold" withString:@""];
            }
        }
        THColorModel *model2 = self.colorArray[1];
        model2.colorName = model.colorName;
        [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    } else if (indexPath.item == 1 && ![model.colorName containsString:@"#"]) {
        model.isSelected = !model.isSelected;
        if (model.isSelected) {
            if ([model.colorName containsString:@"Bold"]) {
                model.colorName = [model.colorName stringByReplacingOccurrencesOfString:@"-Bold" withString:@"-BoldItalic"];
            } else {
                model.colorName = [model.colorName stringByReplacingOccurrencesOfString:@"MT" withString:@"-ItalicMT"];
            }
        } else {
            if ([model.colorName containsString:@"Bold"]) {
                model.colorName = [model.colorName stringByReplacingOccurrencesOfString:@"Italic" withString:@""];
            } else {
                model.colorName =[model.colorName stringByReplacingOccurrencesOfString:@"-Italic" withString:@""];
            }
        }
        THColorModel *model1 = self.colorArray[0];
        model1.colorName = model.colorName;
        [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    } else {
        for (int i = self.isShowFont ? 2 : 0; i < self.colorArray.count; i ++) {
            THColorModel *model = self.colorArray[i];
            model.isSelected = indexPath.item == i ? YES : NO;
        }
        [collectionView reloadData];
    }
    if (self.th_collectionViewDidSelectItemBlock) {
        self.th_collectionViewDidSelectItemBlock(model);
    }
}

- (void)setColorArray:(NSMutableArray *)colorArray {
    _colorArray = colorArray;
    NSMutableArray *array = [colorArray mutableCopy];
    for (int i = 0; i < array.count; i ++) {
        THColorModel *model = array[i];
        if (!self.isShowFont && ![model.colorName containsString:@"#"]) {
            [colorArray removeObject:model];
            continue;
        }
        if (i != 0 && i != 1 && model.isSelected) {
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
            break;
        }
    }
    [self.collectionView reloadData];
}



@end



